<?php

namespace app\common\job;

use think\facade\Log;
use think\queue\Job;

/**
 * 短信发送
 * Class Sms
 * @package app\common\job
 */
class Sms
{
    /**
     * fire是消息队列默认调用的方法
     * @param Job $job 当前的任务对象
     * @param array|mixed $data 发布任务时自定义的数据
     */
    public function fire(Job $job, $data)
    {
        $phone = $data['phone'] ?? '';
        $content = $data['content'] ?? '';
        $var = $data['var'] ?? [];

        $response = $this->getSms()->send($phone, $content, $var);

        if (!$response->isSuccess()) {
            Log::warning('短信发送失败');
        }

        $job->delete();
    }

    private function getSms(): \chuanglan\Sms
    {
        return app( \chuanglan\Sms::class);
    }
}
